package com.medical.device.controller;


import com.medical.device.Application;
import com.medical.device.common.R;
import com.medical.device.entity.Test;
import com.medical.device.entity.WeightTarget;
import com.medical.device.service.ITestService;
import com.medical.device.service.IWeightTargetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yy
 * @since 2020-02-25
 */
@Api(tags = "体重目标")
@RestController
@RequestMapping("/weight-target")
public class WeightTargetController {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    IWeightTargetService iWeightTargetService;

    @ApiOperation(value = "设置用户体重目标", response = String.class)
    @PostMapping("/setWeightTarget")
    public ResponseEntity setWeightTarget(@RequestParam("uid") Integer uid, @RequestParam("weightTarget") String weightTarget) {

        try {
            WeightTarget target = new WeightTarget();
            target.setUid(uid);
            target.setWeightTarget(weightTarget);
            boolean isSuccess = iWeightTargetService.saveOrUpdate(target);
            if (!isSuccess) {
                return new ResponseEntity(R.error("请求参数有误"), HttpStatus.BAD_REQUEST);
            }
            return ResponseEntity.ok(R.success("设置成功"));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ResponseEntity(R.error(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "获取用户体重目标", response = WeightTarget.class)
    @GetMapping("/getWeightTarget")
    public ResponseEntity getWeightTarget(@RequestParam("uid") Integer uid) {

        try {
            WeightTarget weightTarget = iWeightTargetService.getById(uid);
            return ResponseEntity.ok(R.success(weightTarget));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ResponseEntity(R.error(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}

